package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.ScenicQueryObject;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IScenicRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.query.ScenicCommentQuery;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.vo.PicVO;
import cn.wolfcode.luowowo.common.vo.ScenicVO;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.anno.RequiredLogin;
import cn.wolfcode.luowowo.website.anno.UserParm;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/scenic")
public class ScenicController {

    @Reference
    private IScenicService scenicService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IScenicThemeService scenicThemeService;
    @Reference
    private IScenicInsideService scenicInsideService;
    @Reference
    private IScenicDetailService scenicDetailService;
    @Reference
    private IScenicCommentService scenicCommentService;
    @Reference
    private IScenicRedisService scenicRedisService;

    @RequestMapping("")
    public String list(Model model, @ModelAttribute("qo") ScenicQueryObject qo){
        //面包屑查询
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //共享数据
        model.addAttribute("toasts", toasts);
        //移除最后一个
        Destination destination = toasts.remove(toasts.size() - 1);
        //共享数据
        model.addAttribute("dest", destination);
        //查询概要
        Destination dest = destinationService.getByid(qo.getDestId());
        model.addAttribute("dest",dest);
        //热门景点:排行前五的
        List<Scenic> commentTop5 = scenicService.commentTop5(qo.getDestId());
        model.addAttribute("commentTop5",commentTop5);

        //热门景点 收藏数作为评分
        List<Scenic> hostCds=scenicService.getHot();
        //截取,分开显示
        if(hostCds.size() > 9) {
            //得到第一个
            model.addAttribute("hot1", hostCds.get(0));
            //截取索引1-3  java代码遵守前闭后开
            model.addAttribute("hot23", hostCds.subList(1, 3));
            //截取索引3-5
            model.addAttribute("hot45", hostCds.subList(3, 5));
            //截取索引5-7
            model.addAttribute("hot67", hostCds.subList(5, 7));
            //截取索引7
            model.addAttribute("hot8", hostCds.get(7));
            //截取索引8-10
            model.addAttribute("hot0", hostCds.subList(8, 10));
        }
        //查询所有主题景点
        List<ScenicTheme> list = scenicThemeService.listAll();
        model.addAttribute("themes",list);


        //通过高查查询全部景点
        PageInfo pageInfo = scenicService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/scenicspot/list";
    }
    //getView
    @RequestMapping("/getView")
    public String getView(Long themeId,Model model){
        //通过主题id查询所有
        List<Scenic> list = scenicService.selectByThemeId(themeId);
        model.addAttribute("list",list);
        return "/scenicspot/themeList";
    }
    //明细
    @RequestMapping("/detail")
    public String detail(@UserParm UserInfo userInfo, Model model, @ModelAttribute("qo") ScenicCommentQuery qo){
        //判断是否存在用户
        if (userInfo!=null){
            model.addAttribute("userInfo",userInfo);
        }
        //面包屑 同上
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        model.addAttribute("toasts", toasts);
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest", destination);

        //通过景点id来查询景点
        //景点
        Scenic scenic = scenicService.get(qo.getScenicId());
        //System.out.println(qo.getScenicId());

        //通过景点id来查询景点明细
        ScenicDetail scenicDetail = scenicDetailService.get(scenic.getId());
        model.addAttribute("scenicDetail",scenicDetail);
        model.addAttribute("scenic",scenic);
        //通过景点id来查询内部景点
        List<ScenicInside> insides = scenicInsideService.selectByScenicId(qo.getScenicId());
        model.addAttribute("insides",insides);
        //截取8个,为了界面优化
        if (insides.size()>8){
            List<ScenicInside> insides1 = insides.subList(0, 8);
            model.addAttribute("insides",insides1);
        }else {
            model.addAttribute("insides", insides);
        }
        //点评回显
        Page page = scenicCommentService.queryForComments(qo);
        List<ScenicComment> list = page.getContent();
        for (ScenicComment scenicComment : list) {
            String[] split = scenicComment.getPic().split(",");

            List<PicVO> list1 = new ArrayList<>();
            for (String s : split) {
                if (s.length() > 0) {
                    PicVO picVO = new PicVO();
                    picVO.setName(s);
                    list1.add(picVO);
                }
            }
            scenicComment.setPicVO(list1);
        }
            model.addAttribute("page",page);

            //收藏回显
        if (userInfo!=null){
            Boolean isFavor = scenicRedisService.isFavor(scenic.getId(),userInfo.getId());
            //System.out.println(isFavor);
            model.addAttribute("isFavor",isFavor);
        }
            return "/scenicspot/detail";
    }

    //抽出模板后的提取方法
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") ScenicCommentQuery qo){
        //点评回显
        Page page = scenicCommentService.queryForComments(qo);

        List<ScenicComment> list = page.getContent();
        for (ScenicComment scenicComment : list) {
            String[] split = scenicComment.getPic().split(",");

            List<PicVO> list1 = new ArrayList<>();
            for (String s : split) {
                if (s.length() > 0) {
                    PicVO picVO = new PicVO();
                    picVO.setName(s);
                    list1.add(picVO);
                }
            }
            scenicComment.setPicVO(list1);
        }
        model.addAttribute("page",page);
        return "scenicspot/commentTpl";
    }

    //添加评论
    @RequiredLogin
    @ResponseBody
    @RequestMapping("add")
    public Object add(ScenicComment scenicComment, @UserParm UserInfo userInfo){
        scenicCommentService.commentAdd(scenicComment,userInfo);
        return new AjaxResult();
    }

    //文件上传
    @Value("${file.path}")
    private String filePath;

    @ResponseBody
    @RequestMapping("/images")
    public Object upload(MultipartFile pic){
        return UploadUtil.upload(pic,filePath);
    }

    //inside

    /**
     * 内景
     * @param insideId 内景id
     * @param destId 目的地id
     * @param model 共享
     * @return
     */
    @RequestMapping("inside")
    public String inside(Long insideId,Long destId,Model model){
        //面包屑
        List<Destination> toasts = destinationService.getToasts(destId);
        model.addAttribute("toasts", toasts);
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest", destination);

        //通过内景id查询单个
        ScenicInside inside = scenicInsideService.selectByPrimaryKey(insideId);
        model.addAttribute("inside", inside);

        return  "scenicspot/insideDetail";
    }

    /**
     * 收藏
     * @param sid   景点id
     * @param userInfo  用户id
     * @return
     */
    @RequiredLogin
    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid,@UserParm UserInfo userInfo){
        if (userInfo ==null){
            AjaxResult result = new AjaxResult(false,"请登录");
            result.setCode(102);
            return  result;
        }
        //key:sid uid
        boolean flag = scenicRedisService.favor(sid,userInfo.getId());
        scenicRedisService.addCommendScores(RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()),1);
        AjaxResult result = new AjaxResult();
        result.setSuccess(flag);
        return result;
    }


    /**
     * 去过
     * @param sid 景点id
     * @param userInfo 用户
     * @return
     */
    @RequiredLogin
    @RequestMapping("been")
    @ResponseBody
    public Object been(Long sid ,@UserParm UserInfo userInfo){
        //key:sid  uid
        boolean flag = scenicRedisService.been(sid, userInfo.getId());
        scenicRedisService.addCommendScores(RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()),1);
        ScenicVO vo = scenicRedisService.getScenicVO(sid);
        AjaxResult result = new AjaxResult();
        result.setData(vo);
        result.setSuccess(flag);
        return result;
    }

    /**
     * 评论点赞操作
     * @param sid
     * @param userInfo
     * @return
     */
    @RequestMapping("commentThumbUp")
    @ResponseBody
    @RequiredLogin
    public Object commentThumbUp(Long sid, @UserParm UserInfo userInfo){
        boolean flag = scenicRedisService.commentThumbUp(sid,userInfo.getId());
        AjaxResult result = new AjaxResult();
        ScenicVO vo = scenicRedisService.getScenicVO(sid);
        result.setData(vo);
        result.setSuccess(flag);
        return  result;
    }

}
